package com.sdleyou.treepro.common.service.impl;

import com.sdleyou.treepro.common.exception.ServiceException;
import com.sdleyou.treepro.common.model.dao.AchievementDO;
import com.sdleyou.treepro.common.model.enums.ErrorCodeEnum;
import com.sdleyou.treepro.common.model.vo.AchievementVO;
import com.sdleyou.treepro.common.service.AchievementManager;
import com.sdleyou.treepro.common.service.MongoPageManager;
import com.sdleyou.treepro.common.utils.DateUtil;
import com.sdleyou.treepro.common.utils.IdGen;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import java.math.BigDecimal;

@Service
public class AchievementManagerImpl implements AchievementManager {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MongoPageManager mongoPageManager;

    /**
     * 创建成就
     * @param totalTarget
     * @param currentTarget
     * @param totalStep
     */
    @Override
    public void createAchievement(@NotEmpty(message = "全网目标种树量") Long totalTarget,
                                  @NotEmpty(message = "全网当前种树量") Long currentTarget,
                                  @NotEmpty(message = "累计捐赠步数") Long totalStep) {
        AchievementDO achievementDO = new AchievementDO();
        achievementDO.setAId(IdGen.uuid());
        achievementDO.setTotalTarget(totalTarget);
        achievementDO.setCurrentTarget(currentTarget);
        achievementDO.setTotalStep(totalStep);
        achievementDO.setGmtCreate(DateUtil.getDateline());
        achievementDO.setGmtModified(DateUtil.getDateline());
        mongoTemplate.insert(achievementDO);
    }

    /**
     * 更新成就
     * @param aId
     * @param totalTarget
     * @param currentTarget
     * @param totalStep
     */
    @Override
    public void updateAchievement(@NotEmpty(message = "id不能为空") String aId,
                                  @NotEmpty(message = "全网目标种树量") Long totalTarget,
                                  @NotEmpty(message = "全网当前种树量") Long currentTarget,
                                  @NotEmpty(message = "累计捐赠步数") Long totalStep) {
        Query query = new Query(Criteria.where("a_id").is(aId));
        AchievementDO achievementDO = mongoTemplate.findOne(query, AchievementDO.class);
        if (achievementDO == null) {
            throw new ServiceException(ErrorCodeEnum.E900.code(), "成就不存在");
        }
        Update update = new Update();
        if(totalTarget!=null) {
            update.set("total_target", totalTarget);
        }
        if(currentTarget!=null) {
            update.set("current_target", currentTarget);
        }
        if(totalStep!=null) {
            update.set("total_step", totalStep);
        }
        update.set("gmt_modified", DateUtil.getDateline());
        mongoTemplate.updateFirst(query, update, AchievementDO.class);
    }

    /**
     * 获取成就详情
     * @param aId
     * @return
     */
    @Override
    public AchievementVO getAchievement(String aId) {
        Query query = new Query(Criteria.where("a_id").is(aId));
        AchievementDO achievementDO = mongoTemplate.findOne(query, AchievementDO.class);
        AchievementVO achievementVO = new AchievementVO();
        if(achievementDO!=null) {
            BeanUtils.copyProperties(achievementDO, achievementVO);
        }
        return achievementVO;
    }

    /**
     * 获取成就详情
     */
    @Override
    public AchievementVO getAchievement() {
        Query query = new Query();
        AchievementDO achievementDO = mongoTemplate.findOne(query, AchievementDO.class);
        AchievementVO achievementVO = new AchievementVO();
        if(achievementDO!=null) {
            BeanUtils.copyProperties(achievementDO, achievementVO);
        }
        return achievementVO;
    }

}
